package org.jiajin2coding.reggie.common;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Guo Jinhua (jiajin2coding)
 * @version 1.0.0
 * @className ResultVO
 * @description 用于返回统一处理结果
 * 服务端响应的数据最终都会封装成此对象
 * @date 2023-10-24
 */
@Data
public class ResultVO<T> {

    private String code = "0000";   // 编码："0000" 成功，其余的全部表示失败

    private String message = "操作成功";   // 返回的信息

    private T data;   // 返回的数据信息

    private Map<String, String> map = new HashMap<String, String>();   // 动态数据信息

    public static <T> ResultVO<T> success(T data) {
        ResultVO<T> resultVO = new ResultVO<>();
        resultVO.data = data;
        return resultVO;
    }

    public static ResultVO<String> success(String message) {
        ResultVO<String> resultVO = new ResultVO<>();
        resultVO.message = message;
        return resultVO;
    }

    public static <T> ResultVO<T> success(T data, String message) {
        ResultVO<T> resultVO = new ResultVO<>();
        resultVO.message = message;
        resultVO.data = data;
        return resultVO;
    }

    public static <T> ResultVO<T> error(String msg) {
        ResultVO<T> resultVO = new ResultVO<>();
        resultVO.code = "4001";
        resultVO.message = msg;
        return resultVO;
    }

    public static ResultVO<String> error(String code, String msg) {
        ResultVO<String> resultVO = new ResultVO<>();
        resultVO.code = code;
        resultVO.message = msg;
        return resultVO;
    }

    public ResultVO<T> add(String key, String value) {
        this.map.put(key, value);
        return this;
    }

}
